package com.hourupdate.network.client;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.hourupdate.network.bean.ExecuteSQLBean;
import com.hourupdate.network.bean.SendMassageTime;
import com.hourupdate.network.connection.ConnectionJDBC;
import com.hourupdate.network.rss.RssLoadData;
import com.hourupdate.network.utils.Tools;


public class SendMassageClient {
	static final Logger logger = Logger.getLogger(SendMassageClient.class);
	private MyConnectionSocket connect;

	public SendMassageClient() {
//		new Thread(this).start();
	}
	public void run() {
		connect = new MyClientSocket("192.168.1.149", 4002);
		String sql = null;
		ExecuteSQLBean ex = null;
		Connection conn = null;
		PreparedStatement ps = null;
		ArrayList<Map<String, String>> list = null;
		SendMassageTime sendMassageTime = new SendMassageTime();
		sendMassageTime.setSend_massage_id("20130528001");
		try {
			conn = new ConnectionJDBC().getJDBConnection();
			while(true){
				RssLoadData rssLoadData = new RssLoadData();
				rssLoadData.loadData();
				
				sql = "SELECT SEND_MASSAGE_TIME FROM "+ConnectionJDBC.schema+"."+ConnectionJDBC.schemaSEND_MASSAGE_TIME +
						" WHERE SEND_MASSAGE_ID = ?";
				
				ps = conn.prepareStatement(sql);
				ps.setString(1, sendMassageTime.getSend_massage_id());
				ex = new ExecuteSQLBean();
				list = ex.executeQuery(ps);
				ps.close();
				
				if(list != null && list.size() > 0){
					Map<String, String> data  = list.get(0);
					sql = "SELECT CN.CN_ID, CN.CN_LINK, CN.CN_IMAGE, CDN.CDN_ID, " +
							"CDN.CDN_TITLE, CDN.CDN_DESC, CDN.CDN_PUBLISH_DATE, CDN.CDN_LINK, CDN.CDN_IMAGE " +
							"FROM "+ConnectionJDBC.schema+"."+ConnectionJDBC.schemaCHANNEL_DETAIL_NEWS + " AS CDN LEFT JOIN "+
							ConnectionJDBC.schema+"."+ConnectionJDBC.schemaCHANNEL_NEWS + " AS CN "+
							"ON CDN.CN_ID = CN.CN_ID WHERE CDN.CDN_PUBLISH_DATE > ? ORDER BY CDN.CDN_PUBLISH_DATE DESC";
					
					ps = conn.prepareStatement(sql);
					ps.setString(1, data.get("SEND_MASSAGE_TIME"));
					
					ex = new ExecuteSQLBean();
					list = ex.executeQuery(ps);
					ps.close();
					if(list != null && list.size() > 0){
						JSONObject jso_news = new JSONObject();
						JSONArray jsoa = new JSONArray();
						for(Map<String, String> newsDetail : list){
							JSONObject jso = new JSONObject();
							jso.put("cnid", newsDetail.get("CN_ID").toString());
							jso.put("cnlink", newsDetail.get("CN_LINK").toString());
							jso.put("cnlogo", newsDetail.get("CN_IMAGE").toString());
							jso.put("cdnid", newsDetail.get("CDN_ID").toString());
							jso.put("cdntitle", newsDetail.get("CDN_TITLE").toString());
							jso.put("cdndesc", newsDetail.get("CDN_DESC").toString());
							jso.put("cdnlink", newsDetail.get("CDN_LINK").toString());
							jso.put("cdnpubdate", Tools.formatDate(newsDetail.get("CDN_PUBLISH_DATE").toString()));
							jso.put("cdnimage", newsDetail.get("CDN_IMAGE").toString());
							jso.put("banner", "");
							jsoa.put(jso);
						}
						jso_news.put("news", jsoa);
						logger.info(jso_news.toString());
						connect.send(jso_news.toString());
											
						sendMassageTime.setSend_massage_time(list.get(0).get("CDN_PUBLISH_DATE").toString());
						sendMassageTime.updatePreparedStatement(conn);
					}
				}
	
				TimeUnit.MINUTES.sleep(1);
				conn.commit();
			}
		} catch (InterruptedException e) {
			logger.info(e.getMessage());
		}catch (JSONException e) {
			logger.error(e.getMessage());
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}finally {
			try {
				if(!conn.isClosed())conn.close();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
	}
}
